
package com.joysuch.wwyt.audit.support;

import com.joysuch.wwyt.audit.service.AuditLogContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

@Slf4j
public class AopAuditLogDaoInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // System.out.println(this + " execute " + invocation.getMethod().getName());
        Object result = null;
        try {
            String methodName = invocation.getMethod().getName();
            result = invocation.proceed();
            if ("save".equals(methodName) || "saveAll".equals(methodName)) {
                // 保存值
                AuditLogContextHolder.set(result);
            }
        } catch (Throwable throwable) {
            log.error("AopAuditLogDaoInterceptor---------------------error, methodName{}, errorMsg{}", invocation.getMethod().toString(), throwable);
        }
        return result;
    }

}
